package optiondialoges;

import ij.IJ;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.Insets;
import javax.swing.JDialog;
import javax.swing.JComboBox;
import java.awt.GridBagLayout;
import javax.swing.JButton;

import panel.GlyphPanel;

import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SampleImagesOptionsDialog extends JDialog implements
		ActionListener,  Serializable {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JComboBox jComboBox = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JPanel jPanel2 = null;
	private GlyphPanel panel;

	/**
	 * @param owner
	 */
	public SampleImagesOptionsDialog(Frame owner, GlyphPanel panel) {
		super(owner);
		this.panel = panel;
		initialize();
		
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(276, 116);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
			jContentPane.add(getJPanel1(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getJComboBox(), gridBagConstraints);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(getJButton(), gridBagConstraints4);
			jPanel1.add(getJPanel2(), new GridBagConstraints());
			jPanel1.add(getJButton1(), gridBagConstraints4);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			List<String> IJ_Props = readTextFromJar("/IJ_Props.txt");
			ArrayList<String> labels=new ArrayList<String>();
			for (String string : IJ_Props) {
				if (string.startsWith("open")) {
					int s=0,e=0;
					char[] charArray = string.toCharArray();
					for (int i = 0; i < charArray.length; i++) {
						char c = charArray[i];
						if(c=='"')
						{
							if(s==0)s=i+1;
							else  {
								e=i;
								break;
							}
						}else if(c=='['){
							e=i;
							break;
						}
					}
					String substring = string.substring(s, e);
					labels.add(substring);
				}
			}
			jComboBox = new JComboBox();
			for (String string : labels) {
				jComboBox.addItem(string);
			}

		}
		return jComboBox;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Ok");
			jButton.addActionListener(this);
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Cancel");
			jButton1.addActionListener(this);
		}
		return jButton1;
	}

	/**
	 * This method initializes jPanel2
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
		}
		return jPanel2;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		if (button.getText().contentEquals("Ok")) {
			String item = (String) jComboBox.getSelectedItem();
			panel.setCommand(String.format("%nrun(\"%s\");", item));
			panel.setOptionsSet(true);
			panel.setDialog(SampleImagesOptionsDialog.this);
			SampleImagesOptionsDialog.this.setVisible(false);
//			if (owner != null)
//				this.owner.setEnabled(true);
//			else
//				this.getParent().setEnabled(true);
		} else if (button.getText().contentEquals("Cancel")) {
			SampleImagesOptionsDialog.this.setVisible(false);
//			this.owner.setEnabled(true);
		}

	}
	  public List<String> readTextFromJar(String s) {
		    InputStream is = null;
		    BufferedReader br = null;
		    String line;
		    ArrayList<String> list = new ArrayList<String>();

		    try { 
		      is = IJ.class.getResourceAsStream(s);
		      br = new BufferedReader(new InputStreamReader(is));	
		      while (null != (line = br.readLine())) {
		         list.add(line);
		      }
		    }
		    catch (Exception e) {
		      e.printStackTrace();
		    }
		    finally {
		      try {
		        if (br != null) br.close();
		        if (is != null) is.close();
		      }
		      catch (IOException e) {
		        e.printStackTrace();
		      }
		    }
		    return list;
		  }
	

} // @jve:decl-index=0:visual-constraint="10,10"
